/*
 * thermistor_lut.c - Lookup table for NTC thermistor (temperature probe)
 *
 *  Created on: Oct 20, 2017
 *      Author: Oliver Douglas
 */

#include "stdinclude.h"

/* Convert resistance to degrees celsius for the NXRT15WB473FA1B030 thermistor.
 */
const float thermistor_table[] = {
    140.622879,     //R = 1000
    136.688433,     //R = 1100
    133.161338,     //R = 1200
    129.969914,     //R = 1300
    127.059490,     //R = 1400
    124.387473,     //R = 1500
    121.920066,     //R = 1600
    119.630031,     //R = 1700
    117.495112,     //R = 1800
    115.496901,     //R = 1900
    113.620014,     //R = 2000
    111.851465,     //R = 2100
    110.180202,     //R = 2200
    108.596743,     //R = 2300
    107.092896,     //R = 2400
    105.661538,     //R = 2500
    104.296441,     //R = 2600
    102.992126,     //R = 2700
    101.743751,     //R = 2800
    100.547019,     //R = 2900
    99.398095,  //R = 3000
    98.293549,  //R = 3100
    97.230297,  //R = 3200
    96.205559,  //R = 3300
    95.216819,  //R = 3400
    94.261794,  //R = 3500
    93.338409,  //R = 3600
    92.444765,  //R = 3700
    91.579131,  //R = 3800
    90.739915,  //R = 3900
    89.925656,  //R = 4000
    89.135006,  //R = 4100
    88.366723,  //R = 4200
    87.619657,  //R = 4300
    86.892742,  //R = 4400
    86.184990,  //R = 4500
    85.495480,  //R = 4600
    84.823356,  //R = 4700
    84.167818,  //R = 4800
    83.528121,  //R = 4900
    82.903566,  //R = 5000
    82.293498,  //R = 5100
    81.697303,  //R = 5200
    81.114405,  //R = 5300
    80.544262,  //R = 5400
    79.986363,  //R = 5500
    79.440227,  //R = 5600
    78.905399,  //R = 5700
    78.381452,  //R = 5800
    77.867978,  //R = 5900
    77.364596,  //R = 6000
    76.870940,  //R = 6100
    76.386666,  //R = 6200
    75.911448,  //R = 6300
    75.444973,  //R = 6400
    74.986948,  //R = 6500
    74.537090,  //R = 6600
    74.095132,  //R = 6700
    73.660819,  //R = 6800
    73.233909,  //R = 6900
    72.814168,  //R = 7000
    72.401376,  //R = 7100
    71.995320,  //R = 7200
    71.595800,  //R = 7300
    71.202620,  //R = 7400
    70.815595,  //R = 7500
    70.434549,  //R = 7600
    70.059311,  //R = 7700
    69.689718,  //R = 7800
    69.325613,  //R = 7900
    68.966846,  //R = 8000
    68.613273,  //R = 8100
    68.264755,  //R = 8200
    67.921159,  //R = 8300
    67.582356,  //R = 8400
    67.248224,  //R = 8500
    66.918643,  //R = 8600
    66.593499,  //R = 8700
    66.272681,  //R = 8800
    65.956084,  //R = 8900
    65.643604,  //R = 9000
    65.335143,  //R = 9100
    65.030605,  //R = 9200
    64.729899,  //R = 9300
    64.432934,  //R = 9400
    64.139625,  //R = 9500
    63.849889,  //R = 9600
    63.563644,  //R = 9700
    63.280815,  //R = 9800
    63.001324,  //R = 9900
    62.725099,  //R = 10000
    62.452069,  //R = 10100
    62.182167,  //R = 10200
    61.915325,  //R = 10300
    61.651480,  //R = 10400
    61.390568,  //R = 10500
    61.132531,  //R = 10600
    60.877309,  //R = 10700
    60.624844,  //R = 10800
    60.375083,  //R = 10900
    60.127971,  //R = 11000
    59.883457,  //R = 11100
    59.641489,  //R = 11200
    59.402019,  //R = 11300
    59.164998,  //R = 11400
    58.930381,  //R = 11500
    58.698122,  //R = 11600
    58.468177,  //R = 11700
    58.240503,  //R = 11800
    58.015059,  //R = 11900
    57.791805,  //R = 12000
    57.570700,  //R = 12100
    57.351706,  //R = 12200
    57.134787,  //R = 12300
    56.919905,  //R = 12400
    56.707026,  //R = 12500
    56.496114,  //R = 12600
    56.287135,  //R = 12700
    56.080057,  //R = 12800
    55.874848,  //R = 12900
    55.671476,  //R = 13000
    55.469911,  //R = 13100
    55.270123,  //R = 13200
    55.072083,  //R = 13300
    54.875762,  //R = 13400
    54.681133,  //R = 13500
    54.488169,  //R = 13600
    54.296843,  //R = 13700
    54.107129,  //R = 13800
    53.919002,  //R = 13900
    53.732438,  //R = 14000
    53.547413,  //R = 14100
    53.363901,  //R = 14200
    53.181882,  //R = 14300
    53.001332,  //R = 14400
    52.822229,  //R = 14500
    52.644551,  //R = 14600
    52.468278,  //R = 14700
    52.293388,  //R = 14800
    52.119863,  //R = 14900
    51.947681,  //R = 15000
    51.776823,  //R = 15100
    51.607271,  //R = 15200
    51.439006,  //R = 15300
    51.272010,  //R = 15400
    51.106264,  //R = 15500
    50.941753,  //R = 15600
    50.778457,  //R = 15700
    50.616362,  //R = 15800
    50.455449,  //R = 15900
    50.295704,  //R = 16000
    50.137111,  //R = 16100
    49.979653,  //R = 16200
    49.823316,  //R = 16300
    49.668086,  //R = 16400
    49.513946,  //R = 16500
    49.360884,  //R = 16600
    49.208885,  //R = 16700
    49.057936,  //R = 16800
    48.908022,  //R = 16900
    48.759131,  //R = 17000
    48.611250,  //R = 17100
    48.464365,  //R = 17200
    48.318465,  //R = 17300
    48.173537,  //R = 17400
    48.029569,  //R = 17500
    47.886549,  //R = 17600
    47.744466,  //R = 17700
    47.603308,  //R = 17800
    47.463064,  //R = 17900
    47.323723,  //R = 18000
    47.185273,  //R = 18100
    47.047706,  //R = 18200
    46.911009,  //R = 18300
    46.775172,  //R = 18400
    46.640187,  //R = 18500
    46.506042,  //R = 18600
    46.372727,  //R = 18700
    46.240234,  //R = 18800
    46.108553,  //R = 18900
    45.977675,  //R = 19000
    45.847590,  //R = 19100
    45.718289,  //R = 19200
    45.589764,  //R = 19300
    45.462006,  //R = 19400
    45.335007,  //R = 19500
    45.208757,  //R = 19600
    45.083250,  //R = 19700
    44.958476,  //R = 19800
    44.834427,  //R = 19900
    44.711097,  //R = 20000
    44.588476,  //R = 20100
    44.466558,  //R = 20200
    44.345335,  //R = 20300
    44.224799,  //R = 20400
    44.104943,  //R = 20500
    43.985761,  //R = 20600
    43.867245,  //R = 20700
    43.749387,  //R = 20800
    43.632182,  //R = 20900
    43.515623,  //R = 21000
    43.399702,  //R = 21100
    43.284413,  //R = 21200
    43.169751,  //R = 21300
    43.055708,  //R = 21400
    42.942278,  //R = 21500
    42.829456,  //R = 21600
    42.717234,  //R = 21700
    42.605608,  //R = 21800
    42.494571,  //R = 21900
    42.384117,  //R = 22000
    42.274241,  //R = 22100
    42.164937,  //R = 22200
    42.056199,  //R = 22300
    41.948023,  //R = 22400
    41.840402,  //R = 22500
    41.733331,  //R = 22600
    41.626806,  //R = 22700
    41.520820,  //R = 22800
    41.415368,  //R = 22900
    41.310447,  //R = 23000
    41.206050,  //R = 23100
    41.102173,  //R = 23200
    40.998811,  //R = 23300
    40.895959,  //R = 23400
    40.793613,  //R = 23500
    40.691768,  //R = 23600
    40.590418,  //R = 23700
    40.489561,  //R = 23800
    40.389191,  //R = 23900
    40.289304,  //R = 24000
    40.189895,  //R = 24100
    40.090960,  //R = 24200
    39.992496,  //R = 24300
    39.894498,  //R = 24400
    39.796961,  //R = 24500
    39.699882,  //R = 24600
    39.603257,  //R = 24700
    39.507081,  //R = 24800
    39.411351,  //R = 24900
    39.316063,  //R = 25000
    39.221213,  //R = 25100
    39.126798,  //R = 25200
    39.032813,  //R = 25300
    38.939255,  //R = 25400
    38.846121,  //R = 25500
    38.753406,  //R = 25600
    38.661108,  //R = 25700
    38.569222,  //R = 25800
    38.477746,  //R = 25900
    38.386675,  //R = 26000
    38.296007,  //R = 26100
    38.205738,  //R = 26200
    38.115865,  //R = 26300
    38.026385,  //R = 26400
    37.937294,  //R = 26500
    37.848589,  //R = 26600
    37.760268,  //R = 26700
    37.672326,  //R = 26800
    37.584762,  //R = 26900
    37.497572,  //R = 27000
    37.410752,  //R = 27100
    37.324301,  //R = 27200
    37.238214,  //R = 27300
    37.152490,  //R = 27400
    37.067125,  //R = 27500
    36.982117,  //R = 27600
    36.897463,  //R = 27700
    36.813159,  //R = 27800
    36.729204,  //R = 27900
    36.645594,  //R = 28000
    36.562328,  //R = 28100
    36.479402,  //R = 28200
    36.396813,  //R = 28300
    36.314559,  //R = 28400
    36.232639,  //R = 28500
    36.151048,  //R = 28600
    36.069785,  //R = 28700
    35.988846,  //R = 28800
    35.908231,  //R = 28900
    35.827936,  //R = 29000
    35.747959,  //R = 29100
    35.668297,  //R = 29200
    35.588948,  //R = 29300
    35.509911,  //R = 29400
    35.431182,  //R = 29500
    35.352759,  //R = 29600
    35.274641,  //R = 29700
    35.196825,  //R = 29800
    35.119308,  //R = 29900
    35.042089,  //R = 30000
    34.965165,  //R = 30100
    34.888535,  //R = 30200
    34.812196,  //R = 30300
    34.736146,  //R = 30400
    34.660383,  //R = 30500
    34.584905,  //R = 30600
    34.509710,  //R = 30700
    34.434797,  //R = 30800
    34.360163,  //R = 30900
    34.285805,  //R = 31000
    34.211723,  //R = 31100
    34.137915,  //R = 31200
    34.064378,  //R = 31300
    33.991110,  //R = 31400
    33.918110,  //R = 31500
    33.845376,  //R = 31600
    33.772906,  //R = 31700
    33.700699,  //R = 31800
    33.628752,  //R = 31900
    33.557063,  //R = 32000
    33.485632,  //R = 32100
    33.414456,  //R = 32200
    33.343534,  //R = 32300
    33.272863,  //R = 32400
    33.202443,  //R = 32500
    33.132271,  //R = 32600
    33.062347,  //R = 32700
    32.992667,  //R = 32800
    32.923231,  //R = 32900
    32.854037,  //R = 33000
    32.785084,  //R = 33100
    32.716370,  //R = 33200
    32.647893,  //R = 33300
    32.579652,  //R = 33400
    32.511645,  //R = 33500
    32.443871,  //R = 33600
    32.376328,  //R = 33700
    32.309016,  //R = 33800
    32.241931,  //R = 33900
    32.175074,  //R = 34000
    32.108442,  //R = 34100
    32.042034,  //R = 34200
    31.975848,  //R = 34300
    31.909884,  //R = 34400
    31.844140,  //R = 34500
    31.778614,  //R = 34600
    31.713306,  //R = 34700
    31.648213,  //R = 34800
    31.583335,  //R = 34900
    31.518670,  //R = 35000
    31.454216,  //R = 35100
    31.389974,  //R = 35200
    31.325940,  //R = 35300
    31.262114,  //R = 35400
    31.198495,  //R = 35500
    31.135082,  //R = 35600
    31.071872,  //R = 35700
    31.008866,  //R = 35800
    30.946061,  //R = 35900
    30.883457,  //R = 36000
    30.821052,  //R = 36100
    30.758846,  //R = 36200
    30.696836,  //R = 36300
    30.635022,  //R = 36400
    30.573403,  //R = 36500
    30.511977,  //R = 36600
    30.450743,  //R = 36700
    30.389701,  //R = 36800
    30.328849,  //R = 36900
    30.268186,  //R = 37000
    30.207710,  //R = 37100
    30.147422,  //R = 37200
    30.087319,  //R = 37300
    30.027401,  //R = 37400
    29.967666,  //R = 37500
    29.908114,  //R = 37600
    29.848743,  //R = 37700
    29.789553,  //R = 37800
    29.730542,  //R = 37900
    29.671710,  //R = 38000
    29.613055,  //R = 38100
    29.554576,  //R = 38200
    29.496273,  //R = 38300
    29.438144,  //R = 38400
    29.380189,  //R = 38500
    29.322406,  //R = 38600
    29.264794,  //R = 38700
    29.207353,  //R = 38800
    29.150082,  //R = 38900
    29.092979,  //R = 39000
    29.036044,  //R = 39100
    28.979276,  //R = 39200
    28.922674,  //R = 39300
    28.866236,  //R = 39400
    28.809963,  //R = 39500
    28.753853,  //R = 39600
    28.697905,  //R = 39700
    28.642119,  //R = 39800
    28.586493,  //R = 39900
    28.531026,  //R = 40000
    28.475719,  //R = 40100
    28.420570,  //R = 40200
    28.365577,  //R = 40300
    28.310741,  //R = 40400
    28.256060,  //R = 40500
    28.201534,  //R = 40600
    28.147162,  //R = 40700
    28.092943,  //R = 40800
    28.038875,  //R = 40900
    27.984960,  //R = 41000
    27.931194,  //R = 41100
    27.877579,  //R = 41200
    27.824113,  //R = 41300
    27.770794,  //R = 41400
    27.717624,  //R = 41500
    27.664600,  //R = 41600
    27.611721,  //R = 41700
    27.558988,  //R = 41800
    27.506400,  //R = 41900
    27.453955,  //R = 42000
    27.401653,  //R = 42100
    27.349493,  //R = 42200
    27.297475,  //R = 42300
    27.245598,  //R = 42400
    27.193860,  //R = 42500
    27.142262,  //R = 42600
    27.090803,  //R = 42700
    27.039482,  //R = 42800
    26.988297,  //R = 42900
    26.937250,  //R = 43000
    26.886338,  //R = 43100
    26.835561,  //R = 43200
    26.784919,  //R = 43300
    26.734411,  //R = 43400
    26.684036,  //R = 43500
    26.633794,  //R = 43600
    26.583683,  //R = 43700
    26.533704,  //R = 43800
    26.483855,  //R = 43900
    26.434136,  //R = 44000
    26.384547,  //R = 44100
    26.335086,  //R = 44200
    26.285753,  //R = 44300
    26.236548,  //R = 44400
    26.187470,  //R = 44500
    26.138517,  //R = 44600
    26.089691,  //R = 44700
    26.040989,  //R = 44800
    25.992411,  //R = 44900
    25.943958,  //R = 45000
    25.895628,  //R = 45100
    25.847420,  //R = 45200
    25.799334,  //R = 45300
    25.751370,  //R = 45400
    25.703526,  //R = 45500
    25.655803,  //R = 45600
    25.608200,  //R = 45700
    25.560716,  //R = 45800
    25.513350,  //R = 45900
    25.466102,  //R = 46000
    25.418972,  //R = 46100
    25.371959,  //R = 46200
    25.325063,  //R = 46300
    25.278282,  //R = 46400
    25.231616,  //R = 46500
    25.185065,  //R = 46600
    25.138629,  //R = 46700
    25.092306,  //R = 46800
    25.046097,  //R = 46900
    25.000000,  //R = 47000
    24.954015,  //R = 47100
    24.908142,  //R = 47200
    24.862381,  //R = 47300
    24.816729,  //R = 47400
    24.771188,  //R = 47500
    24.725757,  //R = 47600
    24.680435,  //R = 47700
    24.635221,  //R = 47800
    24.590116,  //R = 47900
    24.545118,  //R = 48000
    24.500228,  //R = 48100
    24.455444,  //R = 48200
    24.410766,  //R = 48300
    24.366195,  //R = 48400
    24.321728,  //R = 48500
    24.277367,  //R = 48600
    24.233110,  //R = 48700
    24.188956,  //R = 48800
    24.144906,  //R = 48900
    24.100960,  //R = 49000
    24.057115,  //R = 49100
    24.013373,  //R = 49200
    23.969732,  //R = 49300
    23.926193,  //R = 49400
    23.882755,  //R = 49500
    23.839416,  //R = 49600
    23.796178,  //R = 49700
    23.753039,  //R = 49800
    23.709999,  //R = 49900
    23.667058,  //R = 50000
    23.624215,  //R = 50100
    23.581470,  //R = 50200
    23.538822,  //R = 50300
    23.496270,  //R = 50400
    23.453816,  //R = 50500
    23.411458,  //R = 50600
    23.369195,  //R = 50700
    23.327027,  //R = 50800
    23.284955,  //R = 50900
    23.242977,  //R = 51000
    23.201093,  //R = 51100
    23.159302,  //R = 51200
    23.117605,  //R = 51300
    23.076001,  //R = 51400
    23.034489,  //R = 51500
    22.993070,  //R = 51600
    22.951742,  //R = 51700
    22.910506,  //R = 51800
    22.869360,  //R = 51900
    22.828305,  //R = 52000
    22.787341,  //R = 52100
    22.746466,  //R = 52200
    22.705681,  //R = 52300
    22.664985,  //R = 52400
    22.624377,  //R = 52500
    22.583858,  //R = 52600
    22.543427,  //R = 52700
    22.503084,  //R = 52800
    22.462828,  //R = 52900
    22.422659,  //R = 53000
    22.382577,  //R = 53100
    22.342581,  //R = 53200
    22.302670,  //R = 53300
    22.262846,  //R = 53400
    22.223106,  //R = 53500
    22.183452,  //R = 53600
    22.143881,  //R = 53700
    22.104396,  //R = 53800
    22.064993,  //R = 53900
    22.025675,  //R = 54000
    21.986440,  //R = 54100
    21.947287,  //R = 54200
    21.908217,  //R = 54300
    21.869229,  //R = 54400
    21.830324,  //R = 54500
    21.791499,  //R = 54600
    21.752756,  //R = 54700
    21.714094,  //R = 54800
    21.675512,  //R = 54900
    21.637011,  //R = 55000
    21.598590,  //R = 55100
    21.560248,  //R = 55200
    21.521986,  //R = 55300
    21.483802,  //R = 55400
    21.445698,  //R = 55500
    21.407672,  //R = 55600
    21.369724,  //R = 55700
    21.331853,  //R = 55800
    21.294061,  //R = 55900
    21.256345,  //R = 56000
    21.218707,  //R = 56100
    21.181145,  //R = 56200
    21.143659,  //R = 56300
    21.106250,  //R = 56400
    21.068916,  //R = 56500
    21.031658,  //R = 56600
    20.994474,  //R = 56700
    20.957366,  //R = 56800
    20.920333,  //R = 56900
    20.883373,  //R = 57000
    20.846488,  //R = 57100
    20.809677,  //R = 57200
    20.772939,  //R = 57300
    20.736274,  //R = 57400
    20.699682,  //R = 57500
    20.663163,  //R = 57600
    20.626716,  //R = 57700
    20.590341,  //R = 57800
    20.554039,  //R = 57900
    20.517807,  //R = 58000
    20.481648,  //R = 58100
    20.445559,  //R = 58200
    20.409541,  //R = 58300
    20.373593,  //R = 58400
    20.337716,  //R = 58500
    20.301909,  //R = 58600
    20.266172,  //R = 58700
    20.230504,  //R = 58800
    20.194905,  //R = 58900
    20.159376,  //R = 59000
    20.123915,  //R = 59100
    20.088522,  //R = 59200
    20.053198,  //R = 59300
    20.017942,  //R = 59400
    19.982754,  //R = 59500
    19.947633,  //R = 59600
    19.912580,  //R = 59700
    19.877593,  //R = 59800
    19.842673,  //R = 59900
    19.807820   //R = 60000
};

/* Resistance measurement diagram:
 *
 *
 *   3.3v *--^v^v----*---^v^v--* GND
 *          r_const  |   r_therm
 *                  vin
 */

#define TABLE_STEP_OHMS     100
#define ADC_VOLTS_PER_BIT   (3.0 / 4096)
#define RCONST_OHMS         10000.0

static size_t table_size = sizeof(thermistor_table) / sizeof(thermistor_table[0]);


/* Determine temperature from NTC thermistor voltage divider, using linear
 * interpolation of the lookup table above.
 */
float celsius_from_adc_raw(uint32_t raw)
{
    float vin = raw * ADC_VOLTS_PER_BIT;
    float r_therm = RCONST_OHMS * vin / (3.3 - vin);

    int lower_index = r_therm / TABLE_STEP_OHMS - 1;
    int upper_index = r_therm / TABLE_STEP_OHMS;

    float rv = 0;
    if (lower_index < 0)
        rv = thermistor_table[0];
    else if (upper_index >= table_size)
        rv = thermistor_table[table_size-1];
    else {
        float lower = thermistor_table[lower_index];
        float upper = thermistor_table[upper_index];
        float slope = (upper - lower) / TABLE_STEP_OHMS;
        float dx = r_therm - (lower_index + 1) * TABLE_STEP_OHMS;
        rv = lower + slope * dx;
        return rv;
    }

    return rv;
}
